package mall.controller.admin;

import mall.core.jdbc.Page;
import mall.core.web.Controller;
import mall.entity.Order;
import mall.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
@WebServlet("/admin/order")
public class AdminOrderController extends Controller {

    private final OrderService orderService = new OrderService();

    @Override
    protected Order getParameterForEntity(HttpServletRequest request) {
        Order order = new Order();

        String id = request.getParameter("id");
        if (id != null && !id.isEmpty()) {
            order.setId(Integer.parseInt(id));
        }

        String number = request.getParameter("number");
        if (number != null && !number.isEmpty()) {
            order.setName(number);
        }

        String userId = request.getParameter("userId");
        if (userId != null && !userId.isEmpty()) {
            order.setUserId(Integer.parseInt(userId));
        }

        String name = request.getParameter("name");
        if (name != null && !name.isEmpty()) {
            order.setName(name);
        }

        String address = request.getParameter("address");
        if (address != null && !address.isEmpty()) {
            order.setAddress(address);
        }

        String phone = request.getParameter("phone");
        if (phone != null && !phone.isEmpty()) {
            order.setPhone(phone);
        }

        String status = request.getParameter("status");
        if (status != null && !status.isEmpty()) {
            order.setStatus(Integer.valueOf(status));
        }


        return order;
    }

    /**
     * 新增
     */
    public void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // todo
        redirect("/admin/order?method=viewList", request, response);
    }

    /**
     * 删除
     */
    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // todo
        redirect("/admin/order?method=viewList", request, response);
    }

    /**
     * 修改
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // todo
        redirect("/admin/order?method=viewList", request, response);
    }

    /**
     * 查询
     */
    public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Page<Order> page = getParameterForPage(request);
        Order order = getParameterForEntity(request);

        orderService.page(page, order);

        request.setAttribute("number", order.getNumber());
        setPageAttribute(request, page);

        forward("/admin/order/list", request, response);
    }

    /**
     * 访问列表页面
     */
    public void viewList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Page<Order> page = getParameterForPage(request);
        Order order = getParameterForEntity(request);

        orderService.page(page, order);

        setPageAttribute(request, page);

        forward("/admin/order/list", request, response);
    }

    /**
     * 访问新增页面
     */
    public void viewCreate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // todo
        forward("/admin/order/create", request, response);
    }

    /**
     * 访问编辑页面
     */
    public void viewEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // todo
        forward("/admin/order/edit", request, response);
    }


    /**
     * 立即支付
     */
    public void pay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Order order = getParameterForEntity(request);
        orderService.pay(order.getId());
        redirect("/admin/order?method=viewList", request, response);
    }

    /**
     * 取消订单
     */
    public void cancel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Order order = getParameterForEntity(request);
        orderService.cancel(order.getId());
        redirect("/admin/order?method=viewList", request, response);
    }
}
